<?php

namespace App\Exceptions;

use App\Services\CommonService;
use Exception;

/**
 * 通用 api。用于业务错误
 */
class ApiException extends Exception
{
    const DEFAULT_MSG = '系统繁忙！';

    protected $code;
    protected $context = [];
    protected $data = [];
    protected $options = [];
    protected $logLevel = 'warning';

    public function __construct($message, $code = null, $data = [], $context = [], $options = [])
    {
        $code = $code ?: CommonService::HTTP_CODE_FAIL;
        $message = $message ?: self::DEFAULT_MSG;

        $this->code = $code;
        $this->context = $context;
        $this->data = $data;
        $this->options = $options;
        $this->logLevel = $options['logLevel'] ?? $this->logLevel;
        parent::__construct($message, $code);
    }

    public function getContext()
    {
        return $this->context;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getOptions()
    {
        return $this->options;
    }

    public function getLogLevel()
    {
        return $this->logLevel;
    }
}
